package com.yipingnai.blog.exception;

import com.yipingnai.blog.model.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Author: 一瓶奶
 * @Url: www.yipingnai.cn
 * @Date: 2020/6/3 20:45
 */

// 处理全局异常
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLException.class)
    public RespBean sqlException(SQLException e){
        e.printStackTrace();
        if(e instanceof SQLIntegrityConstraintViolationException){
            return RespBean.error("该数据有关联数据，操作失败");
        }
        return RespBean.error("数据库异常，操作失败");
    }
    @ExceptionHandler(MultipartException.class)
    public RespBean MaxUploadSize(MultipartException e){
        if(e instanceof MaxUploadSizeExceededException){
            return RespBean.error("上传文件过大！");
        }
        return RespBean.error("上传文件异常");
    }
}
